// pages/myEvaluationList/myEvaluationList.js
import api from '../../utils/api.js'
import {
    showLoading,
    hideLoading
} from '../../utils/util.js'
import { HTTP } from '../../utils/http-pro.js'
import { config } from '../../utils/config'
const http = new HTTP()
const app = getApp();
const user_id = wx.getStorageSync("user_id")
const paramsData = {
    page: 1,
    size: 10,
}
const newParams0 = Object.assign({}, paramsData) //  下拉加载 page加 1
Page({
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        imgUrl: config.imgUrl,
        allOrder: [], //所有待评***价订单列表
        empty: false
    },
    //联系商家
    phoneCall(e) {
        let phone = e.currentTarget.dataset.phone;
        let phone_other = e.currentTarget.dataset.phone_other;

        let phoneNumber = phone || phone_other;
        wx.makePhoneCall({
            phoneNumber: `${phoneNumber}`
        })
    },
    onLoad: function(options) {
        this._getNoEvaluateById(paramsData).then((data) => {
            const allOrder = data.data
            if (allOrder.length == 0) {
                this.setData({
                    empty: true
                })
            }
            this.setData({
                allOrder
            })
        })

    },
    onShow: function() {

        let allOrder = this.data.allOrder;
        //这是 由待评***价列表页 操作评***价按钮进入 评***价页面，提交成功后的返回
        let evaluateData = this.data.evaluateData;
        if (evaluateData) {
            console.log('evaluateData', evaluateData)
            console.log('完成评***价，返回待评***价页面或全部订单页面')

            allOrder.forEach((ele, index) => {
                if (ele.id == evaluateData.oid) {
                    ele.status = 4
                }
            })
            this.setData({
                allOrder
            })
        }

    },
    //获取待评***价列表， 商家确认已自提，y用户确认y提取
    _getNoEvaluateById(params) {
        const user_id = wx.getStorageSync("user_id")
        return http.request({
            url: api.getNoEvaluateById,
            data: {
                id: user_id,
                ...params
            }
        })
    },

    /*导航跳转*/
    gomyAllOrderList() {
        wx.redirectTo({
            url: '/pages/myAllOrderList/myAllOrderList',
        })
    },
    gomyRefundList() {
        wx.redirectTo({
            url: '/pages/myRefundList/myRefundList',
        })
    },
    kefu() {
        wx.navigateTo({
            url: '/pages/kefu/kefu',
        })
    },
    // 去评***价
    goEvaluate(e) {
        const order_id = e.currentTarget.dataset.order_id;
        const order_index = e.currentTarget.dataset.index;
        let evaluateShopInfo = this.data.allOrder[order_index]; // 当前订单信息

        wx.setStorageSync('evaluateShopInfo', evaluateShopInfo)
        wx.navigateTo({
            url: '/pages/userGoEvaluate/userGoEvaluate',
        })
    },
    // // 去评***价
    // goEvaluate(e) {
    //   wx.navigateTo({
    //     url: '/pages/userGoEvaluate/userGoEvaluate',
    //     success: () => {
    //       this.setEvaluateShopInfo(e)
    //     }
    //   })
    // },

    setEvaluateShopInfo(e) {
        let id = e.currentTarget.dataset.order_id
        let allOrder = this.data.allOrder;
        let evaluateShopInfo = allOrder.filter((ele, index) => {
            if (ele.id == id) {
                return ele
            }
        })
        console.log('evaluateShopInfo', evaluateShopInfo)
        wx.setStorageSync('evaluateShopInfo', evaluateShopInfo)
    },
    //订单详情
    orderDetail(e) {
        const order_id = e.currentTarget.dataset.order_id;
        wx.navigateTo({
            url: `/pages/orderDetail/orderDetail?order_id=${order_id}`,
        })
    },
    //再来一单
    getAgainOrder(e) {
        const order_id = e.currentTarget.dataset.order_id;
        const order_index = e.currentTarget.dataset.index;
        let againOrderItem = this.data.allOrder[order_index]; // 当前再来一单的订单信息

        wx.setStorageSync('againOrderItem', againOrderItem)
        if (againOrderItem.type == 0) { // 再来一单的订单信息是菜篮 类型
            wx.navigateTo({
                url: `/pages/order/order?againType=0&order_id=${order_id}`,
            })
        } else if (againOrderItem.type == 1) { // 从菜谱类型 进入 提交订单页
            wx.navigateTo({
                url: `/pages/recommendOrder/recommendOrder?againType=1&order_id=${order_id}`,
            })
        }


    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

        let that = this;
        wx.showLoading({
                title: '加载中',
            })
            // 页数+1
        newParams0.page += 1;
        this._getNoEvaluateById(newParams0).then((newData) => {
            console.log('newData', newData)
            newData = newData.data
            if (newData.length) {
                let allOrder_list = that.data.allOrder;
                for (let i = 0; i < newData.length; i++) {
                    allOrder_list.push(newData[i]);
                }
                // 设置数据
                that.setData({
                    allOrder: allOrder_list
                })
                wx.hideLoading();
            } else {
                console.log('已经到底了')
                    // wx.showToast({
                    //   title: '没有更多订单了',
                    //   icon: "none",
                    //   duration: 1500,
                    //   mask: true
                    // })
                this.setData({
                    loadingTip: true,

                })
                wx.hideLoading();
            }

        }).catch((error) => {
            console.log(error)
        })


    },
    onUnload: function() {
        newParams0.page = 1
    }
})